package com.example.threadpool2.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.threadpool2.config.MyTask;
import com.example.threadpool2.domain.TUserCopy;
import com.example.threadpool2.domain.User;
import com.example.threadpool2.mapper.UserMapper;
import com.example.threadpool2.service.ThreadService;
import com.example.threadpool2.utils.ThreadPoolCreate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author zhuyonggang
 * @DateTime: 2021/8/30 21:13
 */
@Service
public class ThreadServiceImpl extends ServiceImpl<UserMapper, User> implements ThreadService {
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sync() {
        int count = 10000;
        int limit = 500;
        int num = count / 500;
        ThreadPoolExecutor threadPoolExecutor = ThreadPoolCreate.getThreadPoolExecutor();
        for (int i = 0; i < num; i++) {
            Map<String, Object> params = new HashMap<>(16);
            params.put("offset", i*limit);
            params.put("limit", limit);
            List<TUserCopy> userList = userMapper.list(params);
            MyTask myTask = new MyTask("user", userList);
            threadPoolExecutor.execute(myTask);
        }
    }

}
